package com.yx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yx.common.utils.MailUtil;
import com.yx.sys.entity.Verification;
import com.yx.sys.mapper.VerificationMapper;
import com.yx.sys.service.IVerificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.geom.QuadCurve2D;
import java.util.Date;
import java.util.UUID;

@Service
public class VerificationServiceImpl extends ServiceImpl<VerificationMapper,Verification> implements IVerificationService {

    @Resource
    private VerificationMapper verificationMapper;

    @Override
    public void sendCode(String uEmail) {
        String code = UUID.randomUUID().toString().replace("-","").substring(0,6);
        System.out.println(code);
        QueryWrapper<Verification> wrapper = new QueryWrapper<>();
        wrapper.eq("email",uEmail);
        Verification selectOne = verificationMapper.selectOne(wrapper);
        Verification verification = new Verification();
        verification.setEmail(uEmail);
        verification.setYzm(code);
        verification.setCreateTime(new Date());
        if(selectOne==null){
            System.out.println(verification);
            verificationMapper.insert(verification);
        }else{
            LambdaUpdateWrapper<Verification> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Verification::getEmail,uEmail);
            verificationMapper.update(verification,updateWrapper);
        }
        new MailUtil(uEmail,code).run();
    }
}
